<?php
namespace app\sign\controller;

use think\Controller;
use app\sign\model\course;
use app\sign\model\Signlist;
use app\sign\model\cossign;
use app\sign\model\mixsign;
use app\sign\model\mapsign;
use app\sign\model\qrsign;
use app\sign\model\photosign;

class Sign extends controller
{
    public function signlist(){
        $courseId = $_GET["courseId"];
        $list = new course();
        $data = $list->cossign($courseId);

        die(json_encode($data));
    }

    public function addsign(){
        $inputText = file_get_contents("php://input");
        $inputData = json_decode($inputText,true);

        $course_Id = $inputData["courseId"];
        $signName = $inputData["sname"];
        $beginDate = $inputData["beginDate"];
        $beginTime = $inputData["beginTime"];
        $endDate = $inputData["endDate"];
        $endTime = $inputData["endTime"];
        $signType = $inputData["typeIndex"];

        $k = " ";

        $beginDT = $beginDate.$k.$beginTime;
        $endDT = $endDate.$k.$endTime;
        
        $addsign = new Signlist();
        $add = $addsign->addsign($signName,$beginDT,$endDT,$signType);
        $newsign = $addsign->newsign();
        // dump($newsign->toArray());

        $sign_Id = $newsign->toArray()["0"]["Id"];
        $addcossign = new cossign();
        $ad = $addcossign->addsign($course_Id,$sign_Id); 
        
        if($add === false || $ad === false){
            $retData = [
                "status"=>"fail",
                "message" =>"fail"
            ];
        }else{
            $retData = [
                "status"=>"success",
                "message" =>"success",
                "signType" =>$signType
            ];
        }

        die(json_encode($retData));
        return $retData;
    }

    public function ismapsign(){
        $user_Id = $_GET["userId"];
        $sign_Id = $_GET["signId"];

        $list = new mapsign();
        $data = $list->issign($user_Id,$sign_Id);
        if($data->toArray()){
            $retData = [
                "status"=>true,
                "message" =>"success"
            ];
        }else{
            $retData = [
                "status"=>false,
                "message" =>"fail"
            ];
        }

        die(json_encode($retData));
    }

    public function mapsign(){
        $signText = file_get_contents("php://input");
        $signData = json_decode($signText,true);

        $user_Id = $signData["userId"];
        $sign_Id = $signData["signId"];
        $sign_Time = $signData["localdate"];
        $address = $signData["signaddress"];

        $addsign = new mapsign();
        $add = $addsign->sign($user_Id,$sign_Id,$sign_Time,$address);
        
        if($add === false){
            $retData = [
                "status"=>"fail",
                "message" =>"fail"
            ];
        }else{
            $retData = [
                "status"=>"success",
                "message" =>"success"
            ];
        }

        die(json_encode($retData));
        return $retData;
    }

    public function mapsignlist(){
        $sign_Id = $_GET["signId"];

        $mixsign = new mapsign();
        $data = $mixsign->signlist($sign_Id);

        die(json_encode($data));
    }

    public function isqrsign(){
        $user_Id = $_GET["userId"];
        $sign_Id = $_GET["signId"];

        $list = new qrsign();
        $data = $list->issign($user_Id,$sign_Id);
        if($data->toArray()){
            $retData = [
                "status"=>true,
                "message" =>"success"
            ];
        }else{
            $retData = [
                "status"=>false,
                "message" =>"fail"
            ];
        }

        die(json_encode($retData));
    }

    public function qrsign(){
        $user_Id = $_GET["userId"];
        $sign_Id = $_GET["signId"];
        $sign_Time = $_GET["signTime"];

        $addsign = new qrsign();
        $add = $addsign->sign($user_Id,$sign_Id,$sign_Time);

        if($add === false){
            $retData = [
                "status"=>"fail",
                "message" =>"fail"
            ];
        }else{
            $retData = [
                "status"=>"success",
                "message" =>"success"
            ];
        }

        die(json_encode($retData));
        return $retData;
    }

    public function qrsignlist(){
        $sign_Id = $_GET["signId"];

        $mixsign = new qrsign();
        $data = $mixsign->signlist($sign_Id);

        die(json_encode($data));
    }
    
    public function isphotosign(){
        $user_Id = $_GET["userId"];
        $sign_Id = $_GET["signId"];

        $list = new photosign();
        $data = $list->issign($user_Id,$sign_Id);
        if($data->toArray()){
            $retData = [
                "status"=>true,
                "message" =>"success"
            ];
        }else{
            $retData = [
                "status"=>false,
                "message" =>"fail"
            ];
        }

        die(json_encode($retData));
    }

    public function photosign(){
        $files = $_FILES["file"];
        $file = $files["name"];
        
        $user_Id = $_POST["user_Id"];
        $sign_Id = $_POST["sign_Id"];
        $sign_Time = $_POST["sign_Time"];

        $addsign = new photosign();
        $add = $addsign->sign($user_Id,$sign_Id,$sign_Time,$file);

        if($add === false){
            $retData = [
                "status"=>"fail",
                "message" =>"fail"
            ];
        }else{
            $retData = [
                "status"=>"success",
                "message" =>"success"
            ];
        }

        die(json_encode($retData));
        return $retData;

    }

    public function photosignlist(){
        $sign_Id = $_GET["signId"];

        $mixsign = new photosign();
        $data = $mixsign->signlist($sign_Id);

        die(json_encode($data));
    }

    public function ismixsign(){
        $user_Id = $_GET["userId"];
        $sign_Id = $_GET["signId"];

        $list = new mixsign();
        $data = $list->issign($user_Id,$sign_Id);
        if($data->toArray()){
            $retData = [
                "status"=>true,
                "message" =>"success"
            ];
        }else{
            $retData = [
                "status"=>false,
                "message" =>"fail"
            ];
        }

        die(json_encode($retData));
    }

    public function mixsign(){
        $files = $_FILES["file"];
        $file = $files["name"];
        
        $user_Id = $_POST["user_Id"];
        $sign_Id = $_POST["sign_Id"];
        $sign_Time = $_POST["sign_Time"];
        $address = $_POST["signaddress"];

        $addsign = new mixsign();
        $add = $addsign->sign($file,$user_Id,$sign_Id,$sign_Time,$address);
        if($add === false){
            $retData = [
                "status"=>"fail",
                "message" =>"fail"
            ];
        }else{
            $retData = [
                "status"=>"success",
                "message" =>"success"
            ];
        }

        die(json_encode($retData));
        return $retData;
    }

    public function mixsignlist(){
        $sign_Id = $_GET["signId"];

        $mixsign = new mixsign();
        $data = $mixsign->signlist($sign_Id);

        die(json_encode($data));
    }
}